package cn.wen233.sunglow.handlers;

import cn.wen233.basic.exceptions.BaseException;
import cn.wen233.basic.http.RestResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 异常统一拦截
 *
 * @author wenei
 * @date 2021-10-18 20:36
 */
@RestControllerAdvice
public class ExceptionApi {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionApi.class);

    @ExceptionHandler(BaseException.class)
    public RestResponse<?> baseException(BaseException e) {
        LOGGER.warn("BaseException异常抛出：", e);
        return RestResponse.failure(e.getMessage());
    }

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public RestResponse<?> sqlIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException e) {
        LOGGER.warn("数据库规约异常异常抛出：" + e.getMessage());
        return RestResponse.failure("数据库规约异常：" + e.getMessage());
    }
}
